﻿using AssistCore;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace Helper
{
    public partial class ArtistHelper : CCWin.Skin_Mac
    {
        public ArtistHelper()
        {
            InitializeComponent();
            this.Icon = Properties.HelpResource.app;
        }
        private int matchType = 1, similar = 1;
        private DateTime runTime = DateTime.Now.AddDays(1);
        Point? startPositon = null, stopPosition = null;
        private Thread artistThread = null;
        private void ArtistHelper_Shown(object sender, EventArgs e)
        {
            Win32Core.RegisterHotKey(this.Handle, 100, 0, (int)Keys.Home);
            var modeDataScoure = new List<object>();
            modeDataScoure.Add(new
            {
                Name = "模式一",
                Value = 1
            });
            modeDataScoure.Add(new
            {
                Name = "模式二",
                Value = 3
            });
            modeDataScoure.Add(new
            {
                Name = "模式三",
                Value = 5
            });
            cb_mode.DataSource = modeDataScoure;
            cb_mode.DisplayMember = "Name";
            cb_mode.ValueMember = "Value";

            var similarDataScoure = new List<object>();
            similarDataScoure.Add(new
            {
                Name = "低",
                Value = 1
            });
            similarDataScoure.Add(new
            {
                Name = "中",
                Value = 2
            });
            similarDataScoure.Add(new
            {
                Name = "高",
                Value = 3
            });
            cb_similar.DataSource = similarDataScoure;
            cb_similar.DisplayMember = "Name";
            cb_similar.ValueMember = "Value";

            cb_mode.SelectedIndex = Properties.HelperSettings.Default.artModel;
            cb_similar.SelectedIndex = Properties.HelperSettings.Default.artModel;
            num_interval.Value = Properties.HelperSettings.Default.artInterval;

        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    HandleHotkey(m);
                    break;
            }
            base.WndProc(ref m);
        }
        private void HandleHotkey(Message m)
        {
            switch (m.WParam.ToInt32())
            {
                case 100:
                    {
                        btn_start.PerformClick();
                        break;
                    }
            }
        }

        private void ArtistHelper_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (artistThread != null && artistThread.IsAlive)
            {
                artistThread.Abort();
            }
            Win32Core.UnregisterHotKey(this.Handle, 100);
            Properties.HelperSettings.Default.artModel = cb_mode.SelectedIndex;
            Properties.HelperSettings.Default.artSimilar = cb_similar.SelectedIndex;
            Properties.HelperSettings.Default.artInterval = num_interval.Value;
            Properties.HelperSettings.Default.Save();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            if (artistThread != null && artistThread.IsAlive)
            {
                StopArtist();
            }
            else
            {
                matchType = int.Parse(cb_mode.SelectedValue.ToString());
                similar = int.Parse(cb_similar.SelectedValue.ToString());
                artistThread = new Thread(StartArtist);
                artistThread.Start();
                btn_start.Text = "停止挂机(Home)";
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
            }
            matchType = int.Parse(cb_mode.SelectedValue.ToString());
            similar = int.Parse(cb_similar.SelectedValue.ToString());
        }
        void StartArtist()
        {
            while (true)
            {
                if (!startPositon.HasValue)
                {
                    startPositon = GameAction.FindPropPositon(Properties.HelpResource.artist_start, matchType, similar);
                    if (!startPositon.HasValue)
                    {
                        MessageBox.Show("你没有开启艺人身份，请开启身份或更换模式后再试", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        StopArtist();
                        return;
                    }
                }
                runTime = DateTime.Now;
                CDD.DDContext.mov(startPositon.Value.X, startPositon.Value.Y);
                Thread.Sleep(500);
                CDD.DDContext.btn(1);
                CDD.DDContext.btn(2);
                Thread.Sleep(500);
                if (!stopPosition.HasValue)
                {
                    stopPosition = GameAction.FindPropPositon(Properties.HelpResource.artist_stop, matchType, similar);
                    if (!stopPosition.HasValue)
                    {
                        MessageBox.Show("无法定位停止位置，请更换模式后再试", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        StopArtist();
                        return;
                    }
                }
                while (true)
                {
                    if ((DateTime.Now - runTime).TotalSeconds >= (double)num_interval.Value)
                    {
                        CDD.DDContext.mov(stopPosition.Value.X, stopPosition.Value.Y);
                        Thread.Sleep(500);
                        CDD.DDContext.btn(1);
                        CDD.DDContext.btn(2);
                        break;
                    }
                }
            }

        }
        void StopArtist()
        {
            Action action = () =>
            {
                btn_start.Text = "开始挂机(Home)";
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
            };
            if (btn_start.InvokeRequired)
            {
                btn_start.Invoke(action);
            }
            else
            {
                action();
            }
            artistThread.Abort();
        }
    }
}
